//
//  SpecialScrollView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/19.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class SpecialScrollView: UIView {
    
    // 赋值
    public var specialTickets: [SpecialTicketInfo]? {
        didSet {
            setNeedsLayout()
        }
    }
    
    private var tickets = ["special1","special2","special3"]
    
    // scrollView
    private lazy var scrollView = UIScrollView()
    // 回调
    public var complete: ((_ item:SpecialTicketInfo) -> Void)?
    
    
    override func layoutSubviews() {
        let width = frame.width * 0.45
        scrollView.backgroundColor = UIColor.white
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        // 尺寸
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.contentSize = CGSize(width: CGFloat(tickets.count) * (width + MARGIN10), height: frame.height)
        // 位置
        let bodyW = width - MARGIN10
        let bodyH = frame.height - MARGIN10 * 4
        for i in 0..<tickets.count {
            let body = SpecialBodyView()
            // 赋值
            if specialTickets != nil {
                let ticket = specialTickets![i]
                body.icon = ticket.picture
                body.time = ticket.departdate
                body.price = ticket.ticketprice
                body.startCity = ticket.city
                body.stopCity = ticket.endcity
            }else {
                body.icon = tickets[i]
            }
            // 阴影
            body.tag = i
            body.layer.cornerRadius = 5
            body.layer.shadowOpacity = 0.2
            body.backgroundColor =  UIColor.init(white: 254 / 255.0, alpha: 1)
            body.layer.shadowColor = UIColor.lightGray.cgColor
            body.layer.shadowOffset = CGSize(width: MARGIN5, height:  MARGIN10)
            body.frame = CGRect(x: CGFloat(i) * (width) + MARGIN, y: 0, width: bodyW, height: bodyH)
            scrollView.addSubview(body)
            // 添加事件
            body.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
        }
        addSubview(scrollView)
    }
    
    @objc func clickItem(btn:UIButton) -> Void {
        guard let tickets = specialTickets else {
            return
        }
        if let callback = complete {
            callback(tickets[btn.tag])
        }
    }
}
